<?php
 require_once('functs.inc');
 require_once('OpenDB.inc');
 require_once('funct.inc');
 include('session.inc');
  include 'color_style.php';
  include LANGUAGE;
  include '../plugins/order/classOrderService.inc.php';
  
 // because this file is used in front end this security-check is disabled:
 //if(!GetAdminID($ID)){echo LOGIN_ERR;exit;}

 do{
 if (!OpenDB()){echo OPEN_DB_ERR; break;}

        $OrderID=$_REQUEST["ID"];
		print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">";
        print "<html>";
        print "<head>";
        print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset="._CHARSET_.";\" >";
        print "<title></title>";

        include 'html_style.php';
        
        print "</head>";
        
         print "<body>";

        print "<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" style=\"width:610px;\">
					<caption><b>".$_SERVER["HTTP_HOST"]." - ".P_TITULOK."".Get_Order_NO($OrderID)."</b></caption>";
        //**********************************************************************
        //zobrazenie udajov o dodavatelovi
        //**********************************************************************
        print "<tr><td>";
            //print "<table border=1 width=100% height=100%>";
            //print "<tr><td>";
            print ("<b><u>".P_DODAVATEL.":</u></b><br />");
			$file = fopen("address.txt", "r") or die (P_SUBOR_ERR." 'address.txt'!");
			while (!feof($file)){
				$file_row = fgets($file, 500);
				print ($file_row. "<br />");
			}
			//print "</td></tr></table>";
        print "</td>";
        //**********************************************************************
        //zobrazenie udajov o odberatelovi
        //**********************************************************************
        print "<td valign=\"top\">";
            //print "<table border=\"1\" width=\"100\"% height=\"100%>";
            //print "<tr><td>";
                print ("<b><u>".P_ODBERATEL.":</u></b><br />");
                //vyhladanie udajov v DB
                @$dbResult=mysql_query("SELECT * FROM order_heads WHERE ID=".$OrderID);
                if (!dbResult) {
                    print P_VYHLADANIE_ERR."(1)";
                    break;
                }
                $sql_array_User = mysql_fetch_object(mysql_query("SELECT * FROM order_heads WHERE ID=".$OrderID));
                if ($sql_array_User->OH_Organisation!="")
                    print $sql_array_User->OH_Organisation."<br />";
                print $sql_array_User->OH_Name." ".$sql_array_User->OH_Surname."<br />";
                print $sql_array_User->OH_Street."<br />";
                print $sql_array_User->OH_PostID." ".$sql_array_User->OH_City;
                if ($sql_array_User->OH_ICO!="")
                    print "<br />".P_ICO.": ".$sql_array_User->OH_ICO;
                if ($sql_array_User->OH_DIC!="")
                    print "<br />".P_DIC.": ".$sql_array_User->OH_DIC;
                //sposob dorucenia
//                switch($sql_array_User->OH_Delivery){
//                    case 0: print "<br /><br />".P_DORUCENIE_1; break;
//                    case 1: print "<br /><br />".P_DORUCENIE_2; break;
//                    case 2: print "<br /><br />".P_DORUCENIE_3; break;
//                }
                $_OrderService = new classOrderService();
                print "<br /><br />".INV_SPOSOB_DORUCENIA.":".$_OrderService->getDeliveryName($sql_array_User->OH_Delivery);
                print "<br />".INV_SPOSOB_PLATBY.":".$_OrderService->getPaymentName($sql_array_User->OH_Payment);
                $Order_text = $sql_array_User->OH_Text;
                mysql_free_result($dbResult);
            //print "</td></tr></table>";
        print "</td></tr>";
        //**********************************************************************
        //zobrazenie tovaru
        //**********************************************************************
        if(_PRODUCT_CODE_SHOWING_=='YES') $CodeCaption = "(".C_KOD.") "; else $CodeCaption = "";
        print "<tr><td colspan=\"2\">";
            print ("<b><u>".P_ZOZNAM_POLOZIEK.":</u></b><br />");
        print "</td></tr>";
        print "<tr><td colspan=\"2\">";
        print "<table width=\"100%\">";
	        if(PAYER_VAT=="YES")
	        //**********************************************************************
	        //platca DPH
	        print "<tr><td style=\"width:220px;margin-right:10px;\"><b>".$CodeCaption.C_POLOZKA."</b></td><td><b>".C_MJ."</b></td><td><b>".C_CENA_ZA_MJ."</b></td><td><b>".C_DPH."</b></td><td><b>".C_CENA_S_DPH."</b></td><td><b>".C_MNOZSTVO."</b></td><td><b>".C_SPOLU."</b></td></tr>";
	        //**********************************************************************
		else
	        //**********************************************************************
	        //platca DPH
	        print "<tr><td style=\"width:270px;margin-right:10px;\"><b>".$CodeCaption.C_POLOZKA."</b></td><td><b>".C_CENA_ZA_MJ."</b></td><td><b>".C_MNOZSTVO."</b></td><td><b>".C_SPOLU."</b></td></tr>";
	        //**********************************************************************
        
        $TotalSum=0;
        $sql_bin= "SELECT * FROM order_products WHERE OH_ID=".$OrderID;
        @$bin_source = mysql_query($sql_bin);
        if (!bin_source) {
            print P_VYHLADANIE_ERR." (2)";
            break;
        }
        while ($sql_array_Prodct = mysql_fetch_object($bin_source))
        {
            print "<tr><td>";
                if(_PRODUCT_CODE_SHOWING_=='YES') print "(".$sql_array_Prodct->OP_Code.") ".$sql_array_Prodct->OP_Name;
                else print $sql_array_Prodct->OP_Name;
                print "</td>";
                print "<td>";
                print $sql_array_Prodct->OP_UnitType;
                print "</td>";
                print "<td>";
                print FormatItAsCurrency($sql_array_Prodct->OP_Price);
                print "</td>";
	                if(PAYER_VAT=="YES"){
		                //**********************************************************************
		                //platca DPH
		                print "<td>";
		                        print $sql_array_Prodct->OP_DPH;
		                print "</td>";
		                print "<td>";
		                        print FormatItAsCurrency(((($sql_array_Prodct->OP_DPH)/100)+1) * $sql_array_Prodct->OP_Price);
		                print "</td>";
		                //**********************************************************************
		            }
                print "<td>";
                print $sql_array_Prodct->OP_Count;
                print "</td>";
                	if(PAYER_VAT=="YES"){
		                //**********************************************************************
						//platca DPH
		                print "<td>";
		                        print FormatItAsCurrency(((($sql_array_Prodct->OP_DPH)/100)+1) * $sql_array_Prodct->OP_Price * $sql_array_Prodct->OP_Count);
		                print "</td>";
		                //**********************************************************************
		            }
		            else{
		                //**********************************************************************
						//neplatca DPH
		                print "<td>";
		                        print FormatItAsCurrency($sql_array_Prodct->OP_Price * $sql_array_Prodct->OP_Count);
		                print "</td>";
		                //**********************************************************************
                	}
            print "</tr>";
            	if(PAYER_VAT=="YES")
		            //**********************************************************************
					//platca DPH
		            $TotalSum+=((($sql_array_Prodct->OP_DPH)/100)+1) * $sql_array_Prodct->OP_Price * $sql_array_Prodct->OP_Count;
		            //**********************************************************************
				else
					//**********************************************************************
					//neplatca DPH
					$TotalSum+=$sql_array_Prodct->OP_Price * $sql_array_Prodct->OP_Count;
					//**********************************************************************
        }

        print "</table>";
        print "</td></tr>";
        //**********************************************************************
        //prepocet rozpisu DPH
        //**********************************************************************
        if(PAYER_VAT=="YES"){ //iba platca DPH
            $aiVATLevels[-1] = 0;
            $sql_bin= "SELECT * FROM order_products WHERE OH_ID=".$OrderID;
            $bin_source = mysql_query($sql_bin);
            while ($sql_array_Prodct = mysql_fetch_object($bin_source))
            {
                if(array_key_exists($sql_array_Prodct->OP_DPH, $aiVATLevels)){
                    $aiVATLevels[$sql_array_Prodct->OP_DPH]+= $sql_array_Prodct->OP_Price * $sql_array_Prodct->OP_Count;
                }else{
                    $aiVATLevels[$sql_array_Prodct->OP_DPH] = $sql_array_Prodct->OP_Price * $sql_array_Prodct->OP_Count;
                }
            }
        }
        //**********************************************************************
        //zobrazenie celkovej sumy
        //**********************************************************************
        print "<tr><td colspan=\"2\" style=\"text-align:right;\">";
            //**********************************************************************
            //platca DPH
            if(PAYER_VAT=="YES"){
                if(count($aiVATLevels)>1){
                    print '
                        <table style="font-size:10px; border:1px dotted black;">
                            <tr>
                                <td>
                                    <b>'.INV_SADZBA.'</b>
                                </td>
                                <td>
                                    <b>'.INV_ZAKLAD_DANE.'</b>
                                </td>
                                <td>
                                    <b>'.O_DPH.'</b>
                                </td>
                                <td>
                                    <b>'.O_SPOLU.'</b>
                                </td>
                            </tr>';

                    foreach($aiVATLevels as $key=>$value){
                        if($key>-1) print '
                            <tr>
                                <td>
                                    '.$key.'%
                                </td>
                                <td>
                                    '.RoundIt($value).' '.O_MENA.'
                                </td>
                                <td>
                                    '.RoundIt($value*($key/100)).' '.O_MENA.'
                                </td>
                                <td>
                                    '.RoundIt($value*(1+($key/100))).' '.O_MENA.'
                                </td>
                            </tr>
                        ';
                    }

                    print '</table>';                    
                }
                print C_CELKOM.":<b>".RoundIt($TotalSum)." ".O_MENA_S_DPH."</b> ";
                //**********************************************************************
            }
	        else{
	            //**********************************************************************
	            //neplatca DPH
	            print C_CELKOM.":<b>".RoundIt($TotalSum)." ".O_MENA."</b>";
                    print '<br>';
                    print '<br>';
                    print O_NIE_SME_PLATCOVIA_DPH."<br />";
                    //**********************************************************************
            }
        print "</td></tr>";
        
        //doplnujuci text
        if($Order_text!="")
            print "
                <tr>
                    <td colspan=\"2\">
                        <br />
                        <u><b>".P_DOPLNUJUCI_TEXT."</b></u><br />
                        $Order_text
                    </td>
                </tr>
            ";

        print "</table>";
        mysql_close();
 }while(false);

?>

<script type="text/javascript">
<!--
	window.print();
-->
</script>


</body>

</html>
